Overview:
- The DataFrame class of pandas library provides multiple ways to query data from a DataFrame instance.
- To get a single value from a DataFrame using the indexes and labels, the properties at and iat are used. To get a group of values by specifying the indexes and labels the properties loc and iloc are used.
- To pop a column from a DataFrame, the method pop() is used.
- To get the first "n" rows and the last "n" rows of the DataFrame, the head() and tail() methods are used.
- To query the data based on Boolean expressions(including specific customizations) methods like query() and asof() are used.
- To iterate over the DataFrame, the method items(), iteritems() are used.
Example 1 - Querying a single value from a pandas DataFrame using indices and labels:
# Example Python program to query a value # Create a DataFrame # Add index and column labels # Query the value from a location using the indices # Query the value from a location using labels |
Output:
DataFrame: |
Example 2 - Querying a group of values from a pandas DataFrame using indices and labels:
# Example Python program that queries tokens = [("ab", "bc", "cd"), df.index = ["r1", "r2", "r3"]; # Query different groups of values using indices r1 = 0; # Query different groups of values using labels |
Output:
DataFrame: |
Example 3 - Pop a column from a DataFrame:
# Example Python program that pops a value # Create a pandas DataFrame df = pds.DataFrame(data=vals); # Remove a column print("DataFrame after popping a column:"); |
Output:
DataFrame: x y z r1 ab bc cd r2 de ef fg r3 gh hi ij Query results using indices [0, 0], [1, 1]: y y r1 bc bc r1 bc bc Query results using indices [0, 1], [1, 2]: y z r1 bc cd r2 ef fg Query results using labels ["r1", "r2"], ["x", "z"]: x z r1 ab cd r2 de fg |
Example 4 - Querying the DataFrame using a Boolean expression:
- The query() method accepts a string containing a Boolean expression. Typically a column name is used against a value or a variable containing a value. To refer to a variable inside the Boolean expression, the symbol @ is prepended to the variable name.
# Example Python program that queries fractions = [(1.1, 1.2, 1.3), # Create a DataFrame print("DataFrame:"); # Query the rows where a column value exceeds a threshold |
Output:
DataFrame: c1 c2 c3 r1 1.1 1.2 1.3 r2 1.4 1.5 1.6 r3 1.7 1.8 1.9 Query results: c1 c2 c3 r2 1.4 1.5 1.6 r3 1.7 1.8 1.9 |
Example 5 - Querying DateTime indexed DataFrame for Non-None rows:
# Example python program that queries # Prices of commodities # Create a pandas DataFrame # Create an index of DateTime instances # Attach the index to the DataFrame # Query the rows that has non-none values between |
Output:
Commodity 1 Commodity 2 Commodity 3 2020-08-05 12:31:10 110.1 250.1 310.1 2020-08-05 12:32:11 NaN 250.0 320.0 2020-08-05 12:33:12 109.6 245.1 312.5 2020-08-05 12:34:13 105.5 NaN NaN 2020-08-05 12:35:14 104.2 249.5 315.2 2020-08-05 12:35:15 107.0 252.5 309.1 Commodity 1 Commodity 2 Commodity 3 2020-08-05 12:31:10 110.1 250.1 310.1 2020-08-05 12:35:14 104.2 249.5 315.2 |